#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <qthread.h>
#include <QtNetwork/QNetworkReply>
#include <QTimer>
#include "ui_mainwindow.h"
#include <QDir>
#include <phonon/AudioOutput>
#include <phonon/MediaObject>
#include <QVariantAnimation>
#include <QTextStream>

#include <QLabel>

//namespace Ui {
//    class MainWindow;
////    class jslp;
//}

#define JVERSION 2.1
#define MAX_GAMERTAGS 100

class JLabel;

struct gamercard {
    QString gamertag;
    JLabel * lbl_avatar;
    QLabel * lbl_info;
    QString info;
    QString status;
    bool online;
    QString pic_url;
    int     gamerscore;
    QString last_game_played_url;
    bool pic_downloaded;
    bool locked;
};

class JLabel : public QLabel {

    Q_OBJECT

public:
    JLabel(QWidget *p=0) : QLabel(p) {}
    QString text;
    void setText(const QString & s)
    { QLabel::setText(s);
      text=s;
    }

signals:
    void clicked( const QString &);

protected:
    void mousePressEvent(QMouseEvent *ev);

};

class JTextAnimation : public QVariantAnimation  {
    Q_OBJECT

public:
    JTextAnimation(QWidget * t) : target(t)
    { this_palette=t->palette(); }


protected:
    void updateCurrentValue ( const QVariant & value );

private:
    QWidget * target;
    QPalette this_palette;

};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void get_info(gamercard &);
    void get_avatar(gamercard &);
    enum SOUNDTYPE { ONLINE };
    void playsound( SOUNDTYPE ,QString);
    bool write_cache();
    bool read_cache();
    void read_state_cache();
private slots:

public slots:
    void source_dmk_clicked();
    void source_pwn_clicked();
    void auto_refresh_clicked();
    void refresh_all_clicked();
    void cache_clicked();
    void avatar_clicked(const QString &);
    void help_clicked();
    void test_clicked();
    void quit();

private:
    Ui::MainWindow *ui;
    QMessageBox msg;
    QTextStream out;
    QNetworkAccessManager * netmanager;
    QList<QString> gamertags;
    QList<gamercard> gamercards;
    QDir configdir;
    QDir soundsdir;
    enum Source {DMK, PWN} info_source;
    int refresh_time_s;
    QTimer refresh_timer;
    Phonon::AudioOutput * audio_output;
    Phonon::MediaObject * media_object;
    bool caching;
    QPoint mLastMousePosition;
    bool mMoving;
    double rounding_p;

protected:
    void resizeEvent(QResizeEvent *event);
    void mouseMoveEvent(QMouseEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);

};

class jslp : public QThread
{
public:
    static void sleep(unsigned long secs) {
        QThread::sleep(secs);
    }
    static void msleep(unsigned long msecs) {
        QThread::msleep(msecs);
    }
    static void usleep(unsigned long usecs) {
        QThread::usleep(usecs);
    }
    static void yield() {
        QThread::yieldCurrentThread();
    }

};

#endif // MAINWINDOW_H
